﻿using LS.DicomViewer.Common;
using LS.Screening.UI.Common;
using ReactiveUI.Fody.Helpers;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace LS.DicomViewer.ViewModels
{
    public class DicomFrameViewModel : ViewModel
    {
        #region 属性/变量

        /// <summary>
        /// 提示信息
        /// </summary>
        [Reactive]
        public string NotifyMessage { get; set; }

        /// <summary>
        /// 文件路径
        /// </summary>
        [Reactive]
        public string DicomFilePath { get; set; }

        /// <summary>
        /// 像素比
        /// </summary>
        [Reactive]
        public double PixelRate { get; set; } = 1;

        /// <summary>
        /// 位图
        /// </summary>
        [Reactive]
        public BitmapSource ImageSource { get; set; }

        /// <summary>
        /// Dicom文件最大帧序号
        /// </summary>
        [Reactive]
        public int MaxFrameIndex { get; set; }

        private DicomInfo currentDicom;
        /// <summary>
        /// 当前Dicom
        /// </summary>
        [Reactive]
        public DicomInfo CurrentDicom { get; set; }

        /// <summary>
        /// 当前浏览帧序号
        /// </summary>
        [Reactive]
        public int CurrentFrameIndex { get; set; } = -1;

        /// <summary>
        /// 取消令牌
        /// </summary>
        private System.Threading.CancellationTokenSource tokenSource = new System.Threading.CancellationTokenSource();

        #endregion 属性/变量

        #region 方法

        #region 加载图片

        /// <summary>
        /// 从路径中加载影像文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="frameTime"></param>
        /// <param name="pixelSpacing"></param>
        /// <returns></returns>
        public async Task Load(string path, double? frameTime, double? pixelSpacing)
        {
            CurrentDicom = null;
            CurrentFrameIndex = -1;
            DicomFilePath = path;
            if (!File.Exists(DicomFilePath)) return;
            await LoadImage(frameTime, pixelSpacing);
        }

        public void Clear()
        {
            CurrentDicom = null;
            ImageSource = null;
            CurrentFrameIndex = -1;
            GC.Collect();
        }

        /// <summary>
        /// 显示图片,给定索引与当前索引相等时不作处理
        /// </summary>
        /// <param name="index"></param>
        public void SetCurrentFrameIndex(int index)
        {
            if (CurrentDicom == null || index >= CurrentDicom.NumberOfFrames || index < 0) return;
            if (CurrentFrameIndex == index) return;
            CurrentFrameIndex = index;
        }

        /// <summary>
        /// 加载影像
        /// </summary>
        /// <param name="frameTime"></param>
        /// <param name="pixelSpacing"></param>
        /// <returns></returns>
        private async Task LoadImage(double? frameTime, double? pixelSpacing)
        {
            if (!File.Exists(DicomFilePath)) return;
            IsBusy = true;
            try
            {
                NotifyMessage = "正在加载Dicom文件，请稍候...";
                CurrentDicom = await DicomViewerHelper.SaveToCache(DicomFilePath, frameTime, pixelSpacing);//108
                MaxFrameIndex = CurrentDicom.NumberOfFrames - 1;
                ImageFilesDir = CurrentDicom.ImageFilesDir;
                CurrentFrameIndex = 0;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                NotifyMessage = string.Empty;
                GC.Collect();
                IsBusy = false;
            }
        }

        public string ImageFilesDir { get; private set; }

        #endregion 加载图片

        #endregion 方法
    }
}